import {defineStore} from 'pinia'
import wxcShop from "@/api/wxc/wxcShop.js";
import wxcSharerLevel from "@/api/wxc/wxcSharerLevel.js";
import {ref} from "vue";

// 租户site状态
const useSiteStore = defineStore('site', () => {
    const isFetchingShop = ref(false)
    const shopDict = ref([])
    const isFetchingSharerLevel = ref(false)
    const sharerLevelDict = ref([])

    /**
     * 获取当前租户已添加的视频号小店字典数据
     */
    const fetchShopDict = async () => {
        try {
            if (!shopDict.value.length && !isFetchingShop.value) {
                isFetchingShop.value = true
                // 通过api获取当前租户已添加的微信小店
                const res = await wxcShop.remote({
                    // 设置查询的字段
                    select: ['app_id', 'shop_id'],
                    // 关联模型
                    relations: [
                        // 定义关联
                        {
                            name: 'basicInfo', // 关联名
                            model: 'App.Wxc.Model.WxcShopInfo', // 关联模型的命名空间，使用 . 代替 \，可咨询后端人员
                            type: 'hasOne', // 关联类型，hasOne：一对一，hasMany：一对多，belongsTo：一对多（反向），belongsToMany：多对多
                            foreignKey: 'shop_id',
                            localKey: 'shop_id',
                        },
                    ]
                })
                if (res.code === 200 && res.success) {
                    const shopList = res.data ?? []
                    for (const idx in shopList) {
                        shopDict.value.push({
                            label: shopList[idx].basic_info.nickname,
                            value: shopList[idx].shop_id
                        })
                    }
                }
            }
            return shopDict.value
        } finally {
            isFetchingShop.value = false
        }
    }
    /**
     * 获取当前租户已添加的等级身份字典数据
     */
    const fetchSharerLevelDict = async () => {
        try {
            if (!sharerLevelDict.value.length && !isFetchingSharerLevel.value) {
                isFetchingSharerLevel.value = true
                const res = await wxcSharerLevel.remote({
                    // 设置查询的字段
                    select: ['level_id', 'title', 'direct_rate', 'indirect_rate'],
                    sort: {sort_id: 'asc'},
                })
                if (res.code === 200 && res.success) {
                    const datalist = res.data ?? []
                    for (const idx in datalist) {
                        sharerLevelDict.value.push({
                            //label: datalist[idx].title + '(' + datalist[idx].desc + ')',
                            label: datalist[idx].title,
                            value: datalist[idx].level_id
                        })
                    }
                }
            }
            return sharerLevelDict.value
        } finally {
            isFetchingSharerLevel.value = false
        }
    }

    return {
        shopDict,
        sharerLevelDict,
        fetchShopDict,
        fetchSharerLevelDict
    }
})

export default useSiteStore
